package com.loma.common.jpush

import android.content.ComponentCallbacks
import android.content.Context
import cn.jpush.android.api.JPushInterface
import cn.jpush.android.ups.JPushUPSManager
import com.loma.common.utils.ext.log

/** 极光推送 **/
object JPush {

    private var mIsInitSuccessful: Boolean = false

    /** 推送是否初始化成功 **/
    val isInitSuccessful: Boolean
        get() = mIsInitSuccessful

    private var mJPushToken: String? = null

    /** JPUSH对应的Token **/
    val jpushToken: String?
        get() = mJPushToken

    @JvmStatic
    fun registerToken(ctx: Context  ,onResult: (Boolean) -> Unit = {} ) = JPushUPSManager.registerToken(ctx, "764e03cbf781e25a3aafd8be", null,"4538f82d116d35bf918fd753") {
        mIsInitSuccessful = it.returnCode == 0
        "mIsInitSuccessful".log()
        if (it.returnCode == 0) {
            mJPushToken = it.token

        }
        onResult(it.returnCode == 0)
    }

    @JvmStatic
    fun unRegisterToken(ctx: Context) = JPushUPSManager.unRegisterToken(ctx) {
        if (it.returnCode == 0) {
            mIsInitSuccessful = false
            mJPushToken = null
        }
    }

    @JvmStatic
    fun turnOnPush(ctx: Context) = JPushUPSManager.turnOnPush(ctx) {
    }

    @JvmStatic
    fun turnOffPush(ctx: Context) = JPushUPSManager.turnOffPush(ctx) {

    }

    @JvmStatic
    fun setDebugEnabled(ctx: Context, enabled: Boolean) = JPushInterface.setDebugMode(enabled)

}